/** 
 * File-Name:Article.java
 *
 * Created on 2011-6-2 上午11:07:36
 * 
 * @author: Neo (neolimeng@gmail.com)
 * Software Engineering Institute, Peking University, China
 * 
 * Copyright (c) 2009, Peking University
 * 
 *
 */
package site.entity;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import site.helper.CourseTitle;

import neoutil.CommonEntity;

/**
 * Description:
 * 
 * @author: Neo (neolimeng@gmail.com) Software Engineering Institute, Peking
 *          University, China
 * @version 1.0 2011-6-2 上午11:07:36
 */
public class Article extends CommonEntity {
	public static int STATUS_UNCHECKED = 0;
	public static int STATUS_CHECKED_NOT_PASS = -1;
	public static int STATUS_CHECKED_PASS = 1;
	public static int STATUS_CHECKED_ONE_PASS = 2;
	public static int STATUS_CHECKED_TWO_PASS = 3;

	private String title;
	private String content;
	private String imagePath;
	private String filePath;
	private long authorId;
	private long categoryId;
	private int cid;
	private Date publishDate;
	private Date updateDate;
	private int status = STATUS_CHECKED_PASS;
	private int visitNum = 0;
	private User author;

	public String getContentAbstract() {
		int contentAbstractLength = 45;
		String contentAbstract = null;

		if (content != null && content.length() > contentAbstractLength) {
			return content.substring(0, contentAbstractLength) + "...";
		} else {
			return contentAbstract;
		}
	}

	// Getters and Setters
	public String getTitle() {
		return title;
	}
	
	private String getShortTitle(int num) {
		String regex = "(<[^>]*>)";
		Matcher matcher = Pattern.compile(regex).matcher(title);
		String str = title;
		while (matcher.find()) {
			str = str.replace(matcher.group(0), "");
		}
		if (str != null & str.length() > num) {
			return str.substring(0, num) + "...";
		} else {
			return str;
		}
	}

	public String getShortTitle15() {
		return getShortTitle(15);
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public long getAuthorId() {
		return authorId;
	}

	public void setAuthorId(long authorId) {
		this.authorId = authorId;
	}

	public User getAuthor() {
		return author;
	}

	public void setAuthor(User author) {
		this.author = author;
		this.authorId = author.getId();
	}

	public Date getPublishDate() {
		return publishDate;
	}

	public void setPublishDate(Date publishDate) {
		this.publishDate = publishDate;
	}

	public Date getUpdateDate() {
		return updateDate;
	}

	public void setUpdateDate(Date updateDate) {
		this.updateDate = updateDate;
	}


	public void setUpdateDate(String updateDate) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		try {
			Date d = sdf.parse(updateDate);
			this.updateDate = d;
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
	
	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public String getImagePath() {
		return imagePath;
	}

	public void setImagePath(String imagePath) {
		this.imagePath = imagePath;
	}

	public long getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(long categoryId) {
		this.categoryId = categoryId;
	}

	public int getCid() {
		return cid;
	}

	public void setCid(int cid) {
		this.cid = cid;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public int getVisitNum() {
		return visitNum;
	}

	public void setVisitNum(int visitNum) {
		this.visitNum = visitNum;
	}
}
